/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package presentation.view;

import java.awt.Component;
import java.util.ArrayList;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableCellEditor;
import javax.swing.table.TableColumn;
import vo.GrupoVO;

/**
 *
 * @author Joanna 8a
 */
public class PanelAñadirMateria extends javax.swing.JPanel {

    /**
     * Creates new form PanelAñadirMateria
     */
    public PanelAñadirMateria() {
        initComponents();
        jSpinnerGrAdMat.setModel(new javax.swing.SpinnerNumberModel(1, 0, 20, 1));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane3 = new javax.swing.JScrollPane();
        jTable1 = new javax.swing.JTable();
        jButtonAmat = new javax.swing.JButton();
        jTextFieldCodigoAmat = new javax.swing.JTextField();
        jLabelCodMatAdMat = new javax.swing.JLabel();
        jTextFieldAmat = new javax.swing.JTextField();
        jLabelNombreAmat = new javax.swing.JLabel();
        jPanelGruposAdMat = new javax.swing.JPanel();
        jLabelNroGrAdMat = new javax.swing.JLabel();
        jSpinnerGrAdMat = new javax.swing.JSpinner();
        jScrollPane4 = new javax.swing.JScrollPane();
        jTable2 = new javax.swing.JTable();
        jButtonGdGrs = new javax.swing.JButton();
        jButtonAdGr = new javax.swing.JButton();
        jButtonQuitarGrAmat = new javax.swing.JButton();
        jScrollPane5 = new javax.swing.JScrollPane();
        jTableGrNumSal = new javax.swing.JTable();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTableGrsAdMat = new javax.swing.JTable();
        jButtonAñPr = new javax.swing.JButton();

        jTable1.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane3.setViewportView(jTable1);

        setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Adición de Materias", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Segoe UI Light", 1, 14), new java.awt.Color(204, 0, 204))); // NOI18N

        jButtonAmat.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jButtonAmat.setText("Añadir Materia");

        jTextFieldCodigoAmat.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N

        jLabelCodMatAdMat.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jLabelCodMatAdMat.setText("Código De La Materia");

        jTextFieldAmat.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N

        jLabelNombreAmat.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jLabelNombreAmat.setText("Nombre de la Materia");

        jPanelGruposAdMat.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "Grupos", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, new java.awt.Font("Segoe UI Light", 1, 14))); // NOI18N

        jLabelNroGrAdMat.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jLabelNroGrAdMat.setText("Número de Grupos");

        jSpinnerGrAdMat.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jSpinnerGrAdMat.setModel(new javax.swing.SpinnerNumberModel(1, 1, 20, 1));

        jTable2.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        jScrollPane4.setViewportView(jTable2);

        jButtonGdGrs.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jButtonGdGrs.setText("Guardar Grupos");

        jButtonAdGr.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jButtonAdGr.setText("Añadir Grupo");
        jButtonAdGr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAdGrActionPerformed(evt);
            }
        });

        jButtonQuitarGrAmat.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jButtonQuitarGrAmat.setText("Quitar Grupo");
        jButtonQuitarGrAmat.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonQuitarGrAmatActionPerformed(evt);
            }
        });

        jTableGrNumSal.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jTableGrNumSal.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Grupo", "Número de Dias De Clase"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.Integer.class
            };
            boolean[] canEdit = new boolean [] {
                false, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jTableGrNumSal.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jTableGrNumSalMouseClicked(evt);
            }
        });
        jScrollPane5.setViewportView(jTableGrNumSal);

        jTableGrsAdMat.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jTableGrsAdMat.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Número", "Salón", "Día", "Hora", "Duración", "Profesor"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.Double.class, java.lang.String.class, java.lang.Double.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, true, true, true, true, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(jTableGrsAdMat);

        jButtonAñPr.setFont(new java.awt.Font("Segoe UI Light", 0, 14)); // NOI18N
        jButtonAñPr.setText("Añadir Profesor");
        jButtonAñPr.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonAñPrActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelGruposAdMatLayout = new javax.swing.GroupLayout(jPanelGruposAdMat);
        jPanelGruposAdMat.setLayout(jPanelGruposAdMatLayout);
        jPanelGruposAdMatLayout.setHorizontalGroup(
            jPanelGruposAdMatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jScrollPane5, javax.swing.GroupLayout.DEFAULT_SIZE, 513, Short.MAX_VALUE)
            .addComponent(jScrollPane1)
            .addGroup(jPanelGruposAdMatLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabelNroGrAdMat)
                .addGap(46, 46, 46)
                .addComponent(jSpinnerGrAdMat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
            .addGroup(jPanelGruposAdMatLayout.createSequentialGroup()
                .addGap(59, 59, 59)
                .addComponent(jButtonAdGr)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButtonQuitarGrAmat)
                .addGap(85, 85, 85))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanelGruposAdMatLayout.createSequentialGroup()
                .addGap(120, 120, 120)
                .addComponent(jButtonAñPr)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jButtonGdGrs)
                .addGap(29, 29, 29))
        );
        jPanelGruposAdMatLayout.setVerticalGroup(
            jPanelGruposAdMatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelGruposAdMatLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanelGruposAdMatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabelNroGrAdMat)
                    .addComponent(jSpinnerGrAdMat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(10, 10, 10)
                .addComponent(jScrollPane5, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanelGruposAdMatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButtonAdGr)
                    .addComponent(jButtonQuitarGrAmat))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 184, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addGroup(jPanelGruposAdMatLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButtonGdGrs)
                    .addComponent(jButtonAñPr))
                .addGap(217, 217, 217))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabelNombreAmat)
                            .addComponent(jTextFieldAmat, javax.swing.GroupLayout.PREFERRED_SIZE, 476, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabelCodMatAdMat)
                            .addComponent(jTextFieldCodigoAmat, javax.swing.GroupLayout.PREFERRED_SIZE, 476, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jPanelGruposAdMat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(171, 171, 171)
                        .addComponent(jButtonAmat, javax.swing.GroupLayout.PREFERRED_SIZE, 189, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(jLabelNombreAmat)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldAmat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabelCodMatAdMat)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jTextFieldCodigoAmat, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jPanelGruposAdMat, javax.swing.GroupLayout.PREFERRED_SIZE, 521, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButtonAmat)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jButtonAñPrActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAñPrActionPerformed
        // TODO add your handling code here:
        FrameAñadirProfesor frAñPr = new FrameAñadirProfesor();
        frAñPr.setLocationRelativeTo(this);
        frAñPr.setVisible(true);
    }//GEN-LAST:event_jButtonAñPrActionPerformed

    private void jButtonAdGrActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonAdGrActionPerformed
        DefaultTableModel mod = (DefaultTableModel) jTableGrNumSal.getModel();

        GrupoVO vo = new GrupoVO();
        if (mod.getRowCount() > 0) {
            vo.setNumeroGrupo((int) mod.getValueAt(mod.getRowCount() - 1, 0) + 1);
        } else {
            vo.setNumeroGrupo(1);
        }
        grupos.add(vo);

        TableColumn col1 = jTableGrNumSal.getColumnModel().getColumn(1);
        SpinnerEditor spinner = new SpinnerEditor();
        col1.setCellEditor(spinner);
        spinner.stopCellEditing();

        Object[] row = new Object[6];
        row[0] = vo.getNumeroGrupo();
        //row[1] = 1;
        mod.addRow(row);
    }//GEN-LAST:event_jButtonAdGrActionPerformed

    private void jButtonQuitarGrAmatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonQuitarGrAmatActionPerformed
        DefaultTableModel mod = (DefaultTableModel) jTableGrNumSal.getModel();
        if (mod.getRowCount() > 0) {
            mod.removeRow(filaGrupo);
        }
        filaGrupo = 0;
    }//GEN-LAST:event_jButtonQuitarGrAmatActionPerformed

    private void jTableGrNumSalMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jTableGrNumSalMouseClicked
        filaGrupo = jTableGrNumSal.rowAtPoint(evt.getPoint());
        
    }//GEN-LAST:event_jTableGrNumSalMouseClicked

    private void añadirFilasTabla2(int numFilas) {

        TableColumn col = jTableGrsAdMat.getColumnModel().getColumn(5);
        JComboBox combo = new JComboBox();
        combo.addItem("Test1");
        combo.addItem("Test2");
        col.setCellEditor(new DefaultCellEditor(combo));

        DefaultTableModel mod = (DefaultTableModel) jTableGrsAdMat.getModel();
        Object[] fila = new Object[6];
        for (int i = 1; i <= numFilas-mod.getRowCount(); i++) {
            fila[0] = grupos.get(filaGrupo).getNumeroGrupo();
            mod.addRow(fila);
        }
    }

    class SpinnerEditor extends AbstractCellEditor implements TableCellEditor {

        private javax.swing.JSpinner spinner;

        public SpinnerEditor() {
            spinner = new JSpinner();
            SpinnerNumberModel spinnerModel = new javax.swing.SpinnerNumberModel(1, 1, 7, 1);
            spinner.setModel(spinnerModel);
            spinner.addChangeListener(new javax.swing.event.ChangeListener() {

                @Override
                public void stateChanged(ChangeEvent e) {
                    filaGrupo = jTableGrNumSal.rowAtPoint(spinner.getLocation());
                    añadirFilasTabla2((int)spinner.getValue());

                }
            });
        }

        @Override
        public Object getCellEditorValue() {
            return spinner.getValue();
        }

        @Override
        public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
            return spinner;
        }
    }
    private ArrayList<GrupoVO> grupos = new ArrayList<>();
    private int filaGrupo;
    private int filaDia;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonAdGr;
    private javax.swing.JButton jButtonAmat;
    private javax.swing.JButton jButtonAñPr;
    private javax.swing.JButton jButtonGdGrs;
    private javax.swing.JButton jButtonQuitarGrAmat;
    private javax.swing.JLabel jLabelCodMatAdMat;
    private javax.swing.JLabel jLabelNombreAmat;
    private javax.swing.JLabel jLabelNroGrAdMat;
    private javax.swing.JPanel jPanelGruposAdMat;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JScrollPane jScrollPane4;
    private javax.swing.JScrollPane jScrollPane5;
    private javax.swing.JSpinner jSpinnerGrAdMat;
    private javax.swing.JTable jTable1;
    private javax.swing.JTable jTable2;
    private javax.swing.JTable jTableGrNumSal;
    private javax.swing.JTable jTableGrsAdMat;
    private javax.swing.JTextField jTextFieldAmat;
    private javax.swing.JTextField jTextFieldCodigoAmat;
    // End of variables declaration//GEN-END:variables
}
